package me.zhengjie.tools.domain

import cn.hutool.core.bean.BeanUtil
import cn.hutool.core.bean.copier.CopyOptions
import com.fasterxml.jackson.annotation.JsonTypeName
import me.zhengjie.common.utils.JacksonUtil

import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.Id
import jakarta.persistence.Table
import jakarta.validation.constraints.NotBlank
import org.apache.commons.lang3.builder.ToStringBuilder
import org.springframework.data.redis.core.RedisHash


/**
 * 邮件配置类，数据存覆盖式存入数据存
 * @author Kuki Wu
 * @date 2018-12-26
 */
@Entity
@Table(name = "tool_email_config")
@JsonTypeName("EmailConfig")
@RedisHash("emailconfig")
data class EmailConfig(

    @Id
    var id: Long? = null,

    // 邮件服务器SMTP地址
    val host: @NotBlank String? = null,

    // 邮件服务器SMTP端口
    var port: @NotBlank String? = null,

    // 发件者用户名，默认为发件人邮箱前缀
    var user: @NotBlank String? = null,
    var pass: @NotBlank String? = null,

    // 收件人
    @Column(name = "from_user")
    var fromUser: @NotBlank String? = null
){
    annotation class Update

    companion object {
        fun fromJson(json: String): EmailConfig? {
            val obj=JacksonUtil.parseObject(json, EmailConfig::class.java)!!
            return when(obj){
                is EmailConfig -> obj
                else -> null
            }
        }
    }
    fun tojson():String? {
        return JacksonUtil.toJSONString(this)
    }

    fun copy(source: EmailConfig?) {
        BeanUtil.copyProperties(source, this, CopyOptions.create().setIgnoreNullValue(true))
    }
}